/*
镶嵌
*/
window._MINLAY = {

    _MAINLABEL:null,

    _CURRENTEQUIP:null,

    showInlay:function(parentNode){
        var uiObj = _GRM.getNodeObj('inlay');
        var mainLabel = uiObj.node;
        _MINLAY._MAINLABEL = mainLabel;

        if(uiObj.isnew){
            _MINLAY.initButton(mainLabel);
        }

        _MINLAY.clearLabel();

        parentNode.addChild(mainLabel);

        return mainLabel;
    },

    clearLabel:function(){
        var mainLabel = _MINLAY._MAINLABEL.getChildByName('mainLabel');
        var stoneNode = mainLabel.getChildByName('goods');
        var goodsNode = mainLabel.getChildByName('arm');
        var goodsNameNode = mainLabel.getChildByName('name');
        stoneNode.active = false;
        goodsNameNode.active = false;
        goodsNode.getChildByName('img').getComponent(cc.Sprite).spriteFrame = null;
    },

    setEquip:function(equipInfo){
        _MINLAY._CURRENTEQUIP = equipInfo;
        _MINLAY.refreshLabel();
    },

    addStone:function(goodsInfo,goodsIndex,goodsArr){
        //console.log(goodsInfo,goodsIndex,goodsArr);
        if(_MINLAY._CURRENTEQUIP==null){
            _TIPS.showMessage('请先放入需要镶嵌的装备!');
        }else{
            var equip = _MINLAY._CURRENTEQUIP;

            if(!equip.stone||equip.stone.length<=0){
                _TIPS.showMessage('此装备没有合适的镶嵌位!');
                return;
            }

            var goodsConfig = _M[goodsInfo.type];
            if(!goodsConfig||!goodsConfig.isStone){
                _TIPS.showMessage('此物品无法镶嵌!');
            }else{
                var ftype = goodsConfig.ftype;
                for(var i=0;i<equip.stone.length;i++){
                    if(
                        equip.stone[i].ftype==ftype
                        &&
                        (
                            equip.stone[i].value==null
                            ||!equip.stone[i]
                            )
                        ){
                            equip.stone[i].value = goodsInfo.type;
                            if(goodsInfo.nmb>1){
                                goodsInfo.nmb--;
                            }else{
                                goodsArr.splice(goodsIndex,1);
                            }
                            _MINLAY.refreshLabel();
                            _MG.refreshPage(_MG._CURRENTLABEL);
                            return;
                        }
                }
                _TIPS.showMessage('此装备没有合适的镶嵌位!');
            }
        }
    },

    refreshLabel:function(){
        if( _MINLAY._CURRENTEQUIP!=null){
            var equip = _MINLAY._CURRENTEQUIP;
            var equipConfig = _EQUIP[equip.type];
            var mainLabel = _MINLAY._MAINLABEL.getChildByName('mainLabel');
            var stoneNode = mainLabel.getChildByName('goods');
            var goodsNode = mainLabel.getChildByName('arm');
            var goodsNameNode = mainLabel.getChildByName('name');
            
            goodsNameNode.active = true;
            goodsNameNode.getComponent(cc.Label).string = equip.type;
            goodsNode.getChildByName('img').getComponent(cc.Sprite).spriteFrame = _GRM.getEquipFrame(equipConfig.img);

            if(equip.stone&&equip.stone.length>0){
                stoneNode.active = true;
                for(var i=0;i<5;i++){
                    if(equip.stone[i]){
                        stoneNode.getChildren()[i].active = true;
                        _MGD.refreshStone(stoneNode.getChildren()[i],equip.stone[i]);
                    }else{
                        stoneNode.getChildren()[i].active = false;
                    }
                }
            }else{
                stoneNode.active = false;
            }
        }
    },

    initButton:function(mainLabel){

    }

}